import { z } from "zod";
import { prisma } from '../../../service/mysql.js';
import { updateFundTags } from '../../../service/fund.js';

export default {
    api: "addFundTags",
    name: "添加账务标签",
    description: "根据客户提供的信息为指定账务明细添加标签，比如：明细1添加餐饮标签。",
    requestSchema: z.object({
        id: z.number().describe("账务明细ID"),
        tags: z.string().describe("待添加的标签信息，多个使用逗号分隔。"),
    }),
    func: async ({ id, tags }, context) => {
        const transaction = await prisma.t_transaction.findUnique({
            where: {
                id: id,
            }
        });
        if (!transaction) {
            throw new Error(`未找到ID为 ${id} 的交易记录`);
        }

        const dbTags = transaction.tags.split(',');
        const beAddedTags = tags.split(',');
        beAddedTags.forEach(addedTag => {
            if (!dbTags.includes(addedTag)) {
                dbTags.push(addedTag);
            }
        });
        await updateFundTags(id, context.book.id, context.user.id, dbTags);
        return `账务明细ID ${id} 已成功添加标签 ${tags}, 当前标签为 ${dbTags.join(',')}`;
    }
}